package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.activation.MailcapCommandMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentTeacherBean;

// /SpringMVC1907/student/insert.action
// /SpringMVC1907/student/deleteById.action
@Controller
@RequestMapping(value = "/student")
public class StudentController {
	
	@RequestMapping("/getStudentInsertPage")
	public String getStudentInsertPage() {
		// request.getRequestDispather("/WEB-INF/student_info.jsp").forward(req,resp);
		// return "/WEB-INF/jsp/student_insert.jsp";
		// 项目在WEB-INF里面的jsp下面，无法直接访问insert界面，所以只能通过转发跳转到insert界面
		return "student_insert";
	}
	
	@RequestMapping(value = "/insert1")
	public ModelAndView insert1(Student student) {
		System.out.println("StudentController.insert()");
		System.out.println(student);
		// request.setAttribute("student",student);
		ModelAndView modelAndView = new ModelAndView();
		// 将数据放到域对象中，就是放到request这个域对象
		modelAndView.addObject("student",student);
		// request.getRequestDispather("/student_info.jsp").forward(req,resp);
		// modelAndView.setViewName("/WEB-INF/jsp/student_info.jsp");
		modelAndView.setViewName("student_info");// 将界面展示出来
		return modelAndView;
	}
	
	// 默认情况下既可以处理get请求又可以处理post请求
	// 如果希望这个方法只处理post请求，method=RequestMethod.POST
	@RequestMapping(value = "/insert2", method = RequestMethod.POST)
	public String insert2(Student student, Model model) {
		System.out.println("StudentController.insert()");
		System.out.println(student);
		model.addAttribute("student", student);
		
		return "student_info";
	}
	
	@RequestMapping(value = "/insert3", method = RequestMethod.POST)
	public void insert3(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		System.out.println("StudentController.insert()");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = new Student(name,Integer.parseInt(age),gender);
		HttpSession session = request.getSession();// ①
		request.setAttribute("student", student);// 上面的request方法，request域对象得到student
		session.setAttribute("student", student);// ①和这步加起来是放置session值，session域对象得到student
		
		// 分配到info界面，然后转发
		request.getRequestDispatcher("/WEB-INF/jsp/student_info.jsp").forward(request, response);
	}
	
	@RequestMapping(value = "/insert")
	public String insert(String name, Integer age, String gender, Model model) {
		System.out.println("StudentController.insert()");
		Student student = new Student(name, age, gender);
		model.addAttribute("student", student);
		
		return "student_info";
	}
	
	@RequestMapping("/insertStudentAndTeacher")
		public String insertStudentAndTeacher(StudentTeacherBean bean) {
			System.out.println(bean.getStudent());
			System.out.println(bean.getTeacher());
			
			return "student_info";
		}
	
	// /SpringMVC1907/student/deleteById.action?id=2
	@RequestMapping("/deleteById")
	public String deleteById(Integer id) {
		System.out.println("StudentController.deleteById()");
		System.out.println(id);
		
		// 转发到某个jsp界面
		// return "student_info";
		
		// 转发到/student/pageList.action
		//return "forward:/student/pageList.action";
		
		// 重定向
		return "redirect:/student/pageList.action";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public List<Student> pageList(@RequestParam(required = true/*,defaultValue = "1"*/)Integer pageNo,
			@RequestParam(value = "ps", defaultValue = "3")Integer pageSize) {
		System.out.println("StudentController.pageList()");
		
		System.out.println("StudentController.pageList()---");
		System.out.println("pageNo: " + pageNo); 
		System.out.println("pageSize: " + pageSize);
		List<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "张三1", 23, "男");
		Student student2 = new Student(1, "张三2", 23, "男");
		Student student3 = new Student(1, "张三3", 23, "男");
		Student student4 = new Student(1, "张三4", 23, "男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		return list;
	}
	
	@RequestMapping("/pageObj")
	@ResponseBody
	public ServerResponse pageObj() {
		System.out.println("StudentController.pageList()");
		List<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "张三1", 23, "男");
		Student student2 = new Student(1, "张三2", 23, "男");
		Student student3 = new Student(1, "张三3", 23, "男");
		Student student4 = new Student(1, "张三4", 23, "男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		ServerResponse serverResponse = ServerResponse.createSuccess("成功", list);
		
		return serverResponse;
	}
	
	@RequestMapping("/deleteAll") 
		public void deleteAll(Integer[] ids,HttpServletRequest request) {
			System.out.println("StudentController.deleteAll()");
//			String[] idArray = request.getParameterValues("ids");
//			System.out.println(Arrays.toString(idArray));// [1, 2, 3] 
			System.out.println(Arrays.toString(ids));// [1, 2, 3] // 本身封装了ids
		}
	
	@RequestMapping("/checkName")
	@ResponseBody
	public boolean checkName() {
		boolean isExist = true;
		return isExist;// true
	}
	
	@RequestMapping("/checkName1")
	@ResponseBody
	public Map<String, Object> checkName1(){
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isExist = true;
		map.put("isExit",isExist);
		map.put("age",23);
		return map;
//		结果：
//		{
//		 "isExist": true,
//		 "age": 23
//		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
